"""
@name : 11-23 LanIpDetect
@author : hj
@projectname: CJH
"""

import subprocess
from IPy import IP

ipstr = input("请输入192.168.56.1~255 之间的IP：")


def runcmd():
    try:
        ret = subprocess.run(["ping", "-c", "1", ipstr])
        print("ret.returncode:", ret.returncode)
    except TypeError as b:
        print(f"TypeError============{b}")
    else:
        with open("IP.txt", "a") as fp:
            if ret.returncode == 0:
                fp.write(f"{ip}已经被使用" + "\n")
            else:
                fp.write(f"{ip}未被使用" + "\n")


if ipstr.startswith("192.168.56."):
    try:
        ip = IP(ipstr)
        runcmd()
    except ValueError as a:
        print(f"ValueError============{a}")
else:
    print("不是以192.168.56.开头的ip，当前无法检测。")